﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ECare.BUS.Data;
using ECare.Database;
using Newtonsoft.Json;

namespace ECare.BUS
{
    public class RoleInfoHelper
    {

        public static int CreateRole(RoleInfo role)
        {
            try
            {
                using (var context = new Entities())
                {
                    var newrole = new role {roleName = role.RoleName, IsActive = role.IsActive};
                    newrole.RoleMetadata = JsonConvert.SerializeObject(role.Permissions);
                    context.roles.AddObject(newrole);

                    context.SaveChanges();
                    return newrole.roleId;

                }
            }
            catch(Exception ex)
            {
                return -1;
            }
        }

        public static bool UpldateRole(RoleInfo role)
        {
            try
            {
                using (var context = new Entities())
                {
                    var findrole = context.roles.FirstOrDefault(a => a.roleId == role.RoleId);
                    if (findrole == null)
                        return false;

                    findrole.roleName = role.RoleName;
                    findrole.IsActive = role.IsActive;
                    findrole.RoleMetadata = JsonConvert.SerializeObject(role.Permissions);
                    context.SaveChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }



        public static RoleInfo GetRoleById(int RoleId)
        {
            using (var context = new Entities())
            {
                var findrole = context.roles.FirstOrDefault(a => a.roleId == RoleId);
                if (findrole == null)
                    return new RoleInfo();

                var res = new RoleInfo
                {
                    RoleName = findrole.roleName,
                    IsActive = !findrole.IsActive.HasValue || findrole.IsActive.Value
                };

                if (!string.IsNullOrEmpty(findrole.RoleMetadata))
                    res.Permissions = JsonConvert.DeserializeObject<RolePermissions>(findrole.RoleMetadata);
                else
                    res.Permissions = new RolePermissions();
                return res;
            }
        }





        public static List<RoleInfo> GetRoles()
        {
            using (var context = new Entities())
            {
                var roles = context.roles.ToList();
                var res = new List<RoleInfo>();
                foreach (var role in roles)
                {
                    var newrole = new RoleInfo();
                    newrole.RoleId = role.roleId;
                    newrole.RoleName = role.roleName;
                    newrole.IsActive = !role.IsActive.HasValue || role.IsActive.Value;

                    if (!string.IsNullOrEmpty(role.RoleMetadata))
                        newrole.Permissions = JsonConvert.DeserializeObject<RolePermissions>(role.RoleMetadata);
                    else
                        newrole.Permissions = new RolePermissions();
                    res.Add(newrole);
                }
                return res;
            }
        }

    }
}
